/************************************************
info: 对ringbuffer的封装做简单的测试
data: 2022/02/10
author: hlp
************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tcp_ringbuffer.h"

int main()
{
	//申请一个ringbuffer 
	ringbuffer_t * ringbuff = ringbuffer_create(128);
	printf("ringbuffer_create size is : %d \n", ringbuff->size);
	printf("ringbuffer isempty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer isfull [%d] \n", ringbuffer_isfull(ringbuff));
	printf("ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_destroy(ringbuff);
	//判断基本的条件
	ringbuff = ringbuffer_create(129);
	printf("ringbuffer_create size is : %d \n", ringbuff->size);
	printf("ringbuffer isempty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer isfull [%d] \n", ringbuffer_isfull(ringbuff));
	printf("ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_destroy(ringbuff);

	//给ringbuff中塞入一定的数据查看相关的基本信息
	ringbuff = ringbuffer_create(129);
	const char* str_data = "FFFF0D0A<header><tail>0D0AFEFE";
	//给ringbuffer中塞入一定的数据，进行获取查看
	ringbuffer_put(ringbuff, str_data, strlen(str_data));
	printf("set data size is : %lu, ringbuff size is %d \n", strlen(str_data), ringbuff->size);
	printf("ringbuffer isempty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer isfull [%d] \n", ringbuffer_isfull(ringbuff));
	printf("ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	int data_len = ringbuffer_get_len(ringbuff);
	if(data_len == -1)
	{
		printf("error of ringbuff data \n");
	}else
	{
		printf("ringbuff data len is %d \n", data_len);
	}
	char * data_exec;
	data_exec = (char*)malloc(data_len +1);
	memset(data_exec, 0, data_len +1);
	printf("sizeof data_exec is %lu \n", sizeof(data_exec));
	ringbuffer_get(ringbuff, data_exec, data_len +1);
	printf("ringbuff get data is :%s \n",data_exec);
	printf("ringbuffer isempty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer isfull [%d] \n", ringbuffer_isfull(ringbuff));
	printf("ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_destroy(ringbuff);
	if(data_exec !=NULL)
	{
		free(data_exec);
		data_exec = NULL;
	}


	printf("******************test of more package check***********************\n");
	const char* data_str_test = "FFFF0D0A<header><tail>0D0AFEFE";
	const char* data_str_test1 = "FFFF0D0A<header>111<tail>0D0AFEFE";
	const char* data_str_test2 = "FFFF0D0A<header>222<tail>0D0AFEFE";
	const char* data_str_test3 = "FFFF0D0A<header><tail>0D0AFEFE";
	const char* data_str_test4 = "FFFF0D0A<header><tail>0D0AFEFE";
	const char* data_str_error = "FFFF0D0A<header>test error";
	//试着塞多个数据 能取出来吗？
	ringbuff = ringbuffer_create(129);
	ringbuffer_put(ringbuff, data_str_test, strlen(data_str_test));
	printf("put one data. ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("put one data.  ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_put(ringbuff, data_str_test1, strlen(data_str_test1));
	printf("put two data. ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("put two data.  ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_put(ringbuff, data_str_test2, strlen(data_str_test2));
	printf("put three data. ringbuffer used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("put three data.  ringbuffer space len is [%d] \n", ringbuffer_space_len(ringbuff));
	printf("ringbuffer is not empty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer is not full [%d] \n", ringbuffer_isfull(ringbuff));
	int test_get_len = ringbuffer_get_len(ringbuff);
	if(test_get_len == -1)
	{
		printf("error of ringbuff data \n");
	}else
	{
		printf("get ringbuff has data len is %d \n", test_get_len);
	}
	data_exec = (char*)malloc(test_get_len +1);
	memset(data_exec, 0, test_get_len +1);
	ringbuffer_get(ringbuff, data_exec, test_get_len +1);
	printf("ringbuff get data is :%s \n",data_exec);
	printf("get all data used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("get all data space len is [%d] \n", ringbuffer_space_len(ringbuff));
	printf("ringbuffer isempty [%d] \n", ringbuffer_isempty(ringbuff));
	printf("ringbuffer is not full [%d] \n", ringbuffer_isfull(ringbuff));
	ringbuffer_destroy(ringbuff);
	if(data_exec !=NULL)
	{
		free(data_exec);
		data_exec = NULL;
	}

	ringbuff = ringbuffer_create(129);
	ringbuffer_put(ringbuff, data_str_test, strlen(data_str_test));
	ringbuffer_put(ringbuff, data_str_error, strlen(data_str_error));
	test_get_len = ringbuffer_get_len(ringbuff);
	if(test_get_len == -1)
	{
		printf("error of ringbuff data \n");
	}else
	{
		printf("get ringbuff has data len is %d \n", test_get_len);
	}
	ringbuffer_put(ringbuff, data_str_test, strlen(data_str_test));
	test_get_len = ringbuffer_get_len(ringbuff);
	if(test_get_len == -1)
	{
		printf("error of ringbuff data \n");
	}else
	{
		printf("get ringbuff has data len is %d \n", test_get_len);
	}
	data_exec = (char*)malloc(test_get_len +1);
	memset(data_exec, 0, test_get_len +1);
	ringbuffer_get(ringbuff, data_exec, test_get_len +1);
	printf("ringbuff get data is :%s \n",data_exec);
	printf("get all data used len is [%d] \n",ringbuffer_use_len(ringbuff));
	printf("get all data space len is [%d] \n", ringbuffer_space_len(ringbuff));
	ringbuffer_destroy(ringbuff);
	if(data_exec !=NULL)
	{
		free(data_exec);
		data_exec = NULL;
	}
	//根本不会有write追到read的场景，除非这里的设计做成不是全部取出，或者多线程处理
	printf("******************* check ");

	return 0;
}


/***************************************
有关et模式下recv的处理，
	定义缓冲区循环接收
	放入ringbuffer中

	1:接收recv的数据
	2:用不用关闭当前fd，recv如何处理啊
		在recv 返回值0时，说明主动关闭，否则，主动发送时返回-1也是关闭
****************************************/


//这里的设计是不合理的，应该要保持长连接，并且，保证长连接不断开。
